<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/7
 * Time: 9:35
 */

namespace app\sgb\controller\v1;

use app\sgb\controller\BaseCommon;
use app\sgb\logic\MobileLogic;

class Mobile extends BaseCommon
{
    public function __construct()
    {
        parent ::__construct();
        $this -> params = $this -> request;
        $this -> mobile_code = config('mobile');

    }

    /**
     * 发送短信验证码
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function send_code()
    {
        $params = $this -> request;
        $type = $params['send_type'];
        if (empty($type))
            return $this -> ajaxReturn($this -> failError['FFQQ']);
        $mobile = $params['mobile'];
        $mobileLogic = new MobileLogic();
        if (empty($mobile))
            $this -> ajaxReturn($this -> mobile_code['BNWK']);

        if (!isMobile($mobile))
            $this -> ajaxReturn($this -> mobile_code['GSCW']);

        $dataList = db('aide_mobile') -> where(['mobile' => $mobile]) -> find();
        if (!empty($dataList))
            $this -> ajaxReturn($this -> mobile_code['HMCZ']);

        $issetMobile = cache('mobile_' . $mobile);
        if (!empty($issetMobile)) {
            $this -> ajaxReturn($this -> mobile_code['CZPF']);
        }
        //每天不能超过5次
        $sendMob = 'sgb_msg_send_count' . $mobile;
        $sendMsgCount = cache($sendMob);
        if (is_numeric($sendMsgCount) && $sendMsgCount >= 5) {
            return $this -> ajaxReturn($this -> mobile_code['YDSX']);
        }

        $expire_time = strtotime(date('Y-m-d', strtotime("+1 day"))) - time(); //当日剩余时间
        cache($sendMob, intval($sendMsgCount) + 1, $expire_time);

        $code = $mobileLogic -> create_code();
        $msg = sprintf(MobileLogic ::$code_msg, $code);

        switch ($type) {
            case 'aide':
                $sendRes = $mobileLogic -> sendSigleMsg(0, $mobile, $msg, $code);
                if ($sendRes) {
                    return $this -> ajaxReturn($this -> mobile_code['FSCG']);
                } else {
                    return $this -> ajaxReturn($this -> mobile_code['FSSB']);
                }
                break;
        }
    }

    public function check_code()
    {
        $params=$this->request;
        $mobile=$params['mobile'];
        $type=$params['send_type'];
        $code=$params['code'];
        if(empty($mobile)){
            return $this->ajaxReturn($this->failError['CSCW']);
        }
        $cacheCode=cache('mobile_code_'.$mobile);

        if (empty($cacheCode)){
            return $this->ajaxReturn($this->failError['CSCW']);
        }

        if ($cacheCode==trimall($code)){
            switch ($type){
                case 'aide':
                    break;
            }
            return $this->ajaxReturn($this->mobile_code['YZCG']);
        }else{
            return $this->ajaxReturn($this->mobile_code['YZSB']);
        }
    }
}